#include <iostream>
#include <cstring>

using namespace std;

const int N = 3e6 + 10;
int tree[N][62], p[N], e[N], idx;
int T, n, q;

int get_num(char ch)
{
	if(ch >= 'a' && ch <= 'z') return ch - 'a';
	else if(ch >= 'A' && ch <= 'Z') return ch - 'A' + 26;
	else return ch - '0' + 52;
}

void insert(string& s)
{
	int cur = 0;
	p[cur]++;
	for(auto ch : s)
	{
		int path = get_num(ch);
		if(tree[cur][path] == 0) tree[cur][path] = ++idx;
		cur = tree[cur][path];
		p[cur]++;
	}
	e[cur]++;
}
int find_pre(string& s)
{
	int cur = 0;
	for(auto ch : s)
	{
		int path = get_num(ch);
		if(tree[cur][path] == 0) return 0;
		cur = tree[cur][path];
	}
	return p[cur];
}

int main()
{
	cin >> T;
	while(T--)
	{
		for(int i = 0; i <= idx; i++)
		{
			p[i] = 0;
			for(int j = 0; j < 62; j++)
			{
				tree[i][j] = 0;
			}	
		} 
		idx = 0;
		cin >> n >> q;
		for(int i = 1; i <= n; i++)
		{
			string s; cin >> s;
			insert(s);
		}
		while(q--)
		{
			string s; cin >> s;
			cout << find_pre(s) << endl;
		}
	}
	
	return 0;
}
